package cn.wp.ewallet.upms.dao.impl;

import cn.wp.common.core.dao.JdbcSupportTemplate;
import cn.wp.common.core.dao.SqlString;
import cn.wp.ewallet.common.extension.constant.EwalletConstant;
import cn.wp.ewallet.upms.dao.SystemRolesInfoDao;
import cn.wp.ewallet.upms.model.entity.SystemRolesInfoDo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @ClassName SystemRolesInfoDaoImpl
 * @Author ruixule
 * @Description TODO
 * @Date 2019/12/25 14:31
 */
@Repository
public class SystemRolesInfoDaoImpl implements SystemRolesInfoDao {
    @Autowired
    private JdbcSupportTemplate jdbcSupportTemplate;

    @Override
    public List<SystemRolesInfoDo> selectSystemRolesInfoDoBySysUserId(String sysUserId) {
        SqlString sqlString = new SqlString();
        sqlString.add("SELECT * FROM system_roles_info WHERE  EXISTS");
        sqlString.add("  ( SELECT sys_role_id FROM system_user_role_rel WHERE system_user_role_rel.sys_user_id=?  AND system_user_role_rel.STATUS=? ");
        sqlString.add("AND system_roles_info.SYS_ROLE_ID=system_user_role_rel.SYS_ROLE_ID AND system_roles_info.STATUS=? )");
        Object[] params = new Object[]{
                sysUserId,
                "1000",
                "1000"
        };
        return jdbcSupportTemplate.selectList(sqlString.build(), params, SystemRolesInfoDo.class);
    }

    @Override
    public List<SystemRolesInfoDo> getRoleIds(String staffId) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select ro.sys_role_id, ro.SYS_ROLE_NAME");
        sqlString.add("from system_user_info u, system_user_role_rel re, system_roles_info ro");
        sqlString.add("where u.staff_id = ?");
        sqlString.add("    and re.sys_user_id = u.sys_user_id");
        sqlString.add("    and re.sys_role_id = ro.sys_role_id");
        sqlString.add("    and u.status = ?");
        sqlString.add("    and re.status = ?");
        Object[] params = new Object[]{
                staffId,
                EwalletConstant.STATUS,
                EwalletConstant.STATUS
        };
        return jdbcSupportTemplate.selectList(sqlString.build(),params, SystemRolesInfoDo.class);
    }

    @Override
    public List<SystemRolesInfoDo> selectRoleList() {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select sys_role_id,SYS_ROLE_NAME from system_roles_info where status = ?");
        return jdbcSupportTemplate.selectList(sqlString.build(),new Object[]{ EwalletConstant.STATUS},SystemRolesInfoDo.class);
    }
}
